from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
import uvicorn
import os
# 导入配置
from config import Config

# 创建输出文件目录，使用配置文件中的路径
os.makedirs(Config.TEMP_DIR, exist_ok=True)

app = FastAPI(
    title="PaddleOCR PDF处理服务",
    description="使用PaddleOCR处理PDF文件的API服务",
    version="1.0.0"
)

# 配置CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 允许所有来源
    allow_credentials=True,
    allow_methods=["*"],  # 允许所有方法
    allow_headers=["*"],  # 允许所有头
)

# 挂载静态文件目录
app.mount("/files", StaticFiles(directory="output_files"), name="files")

# 注册路由
from routers.router_ocr_gpu_pdf import router as router_ocr_gpu_pdf
from routers.router_ocr_pdf import router as router_pdf
from routers.router_doc_translation import router as router_translation

app.include_router(router_ocr_gpu_pdf, prefix="/router_paddleocr3_gpu_pdf", tags=["GPU PDF处理"])
app.include_router(router_pdf, prefix="/router_paddleocr3_default_pdf", tags=["PDF处理"])
app.include_router(router_translation, prefix="/router_doc_translation", tags=["文档翻译"])

@app.get("/")
async def root():
    return {"message": "欢迎使用PaddleOCR PDF处理服务"}

if __name__ == "__main__":
    # 使用不同的端口，避免端口冲突
    uvicorn.run("main:app", host="0.0.0.0", port=8080, reload=True)